package com.sky.controller.admin;

import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "菜品分类相关接口")
@Slf4j
@Alias("adminCategoryController")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("分类模块的分页查询业务接口")
    @GetMapping("/page")
    public Result getCategoryByPage(CategoryPageQueryDTO dto){

        Result result = categoryService.getCategoryByPage(dto);

        return result;
    }

    @ApiOperation("分类模块的增加功能业务接口")
    @PostMapping
    public Result addCategory(@RequestBody  CategoryDTO dto){

        Result result = categoryService.addCategory(dto);

        return result;

    }

    @ApiOperation("分类模块 启用、禁用分类的业务接口")
    @PostMapping("status/{status}")
    public Result setCategoryStatus(@PathVariable Integer status,@RequestParam Long id){
        Category category = Category.builder().id(id).status(status).build();

        Result result = categoryService.updateCategory(category);

        return result;

    }


    @ApiOperation("分类模块修改分类信息的业务接口")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO dto){
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);

        category.setUpdateUser(BaseContext.getCurrentId());
        BaseContext.removeCurrentId();
        category.setUpdateTime(LocalDateTime.now());

        return categoryService.updateCategory(category);
    }

    @ApiOperation("分类模块 删除分类功能业务接口")
    @DeleteMapping
    public Result deleteCategory(@RequestParam("id") Long id){
        return categoryService.deleteCategoryById(id);
    }

    @ApiOperation("根据类型查询分类的业务接口")
    @GetMapping("list")
    public Result<List<Category>> getCategoryListByType(@RequestParam("type") Long type){
        log.info("分类类型为：{}",type);
        return categoryService.getCategoryListByType(type);
    }

}
